import Vue from 'vue';
import Vuex from 'vuex';
import axios from 'axios'
import VueAxios from 'vue-axios'
Vue.use(VueAxios, axios);

Vue.use(Vuex);


export const store = new Vuex.Store({
    //状态
    state:{
        lists:[],
        comments:[],
        products:[]
    },
    //获取状态
    getters:{
        lists:state=>{
            return state.lists
        },
        comments:state=>{
            return state.comments
        },
        products:state=>{
            return state.products
        }
    },
    //修改状态
    mutations:{
        checkLists:(state,playload)=>{
            for(let i=0;i<playload.length;i++){
                state.lists.push(playload[i]);
            }
        },
        checkComments:(state,playload)=>{
            
                state.comments=playload;
        },
        checkProducts:(state,playload)=>{
            
                state.products=playload;
        }

    },
    //异步分发
    actions:{
        checkLists:({commit},page)=>{
            axios.post('http://localhost:3000/news/list',page)
            .then(res=>{
                commit('checkLists',res.data.rows)
              
            })	
        },
        checkComments:({commit},id)=>{
            axios.post('http://localhost:3000/comment/list',id)
            .then(res=>{
            
                commit('checkComments',res.data.rows)
              
            })	
        },
        checkProducts:({commit})=>{
            axios.post('http://localhost:3000/product/list',{rows:100})
            .then(res=>{
                console.log(res);
                
                commit('checkProducts',res.data.rows)
              
            })	
		}
    }
})